// 带有验证码
<template>
  <div class="logincontainer">
    <el-form
      ref="loginForm"
      class="loginForm"
      :model="loginForm"
      :inline="false"
      size="normal"
      :rules="rules"
    >
      <el-form-item>
        <div class="loginTitle">系统登录</div>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item prop="code">
        <el-row :gutter="20">
          <el-col :span="16" :offset="0">
            <el-input
              v-model="loginForm.code"
              placeholder="请输入验证码"
            />
          </el-col>
          <el-col :span="8" :offset="0">
            <img :src="imgSrc" class="imgse" @click="getImage">
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-button
              class="btn"
              type="primary"
              size="default"
              @click="handlerLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-button
              class="btn"
              type="default"
              size="default"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getImageApi } from '@/api/user'
export default {
  // 存储数据的地方，页面需要显示的数据，都需要显示的在data里面定义
  // 双向绑定的

  data() {
    return {
      loading: false,
      imgSrc: '',
      // 登录数据域
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      // 表单验证规则
      rules: {
        username: [
          {
            required: true,
            trigger: 'change',
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'change',
            message: '请输入密码'
          }
        ],
        code: [
          {
            required: true,
            trigger: 'change',
            message: '请输入验证码'
          }
        ]
      }
    }
  },
  created() {
    this.getImage()
  },
  methods: {
    // 获取验证码
    async getImage() {
      this.imgSrc = await getImageApi()
    },
    // 登录提交事件
    handlerLogin() {
      this.$refs.loginForm.validate((valid) => {
        // 2.如果表单验证通过
        if (valid) {
          this.loading = true
          // 3.调用登录
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.logincontainer {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginForm {
    height: 350px;
    width: 450px;
    box-shadow: 0 0 25px #cac6c6;
    border-radius: 10px;
    padding: 20px 35px;
    .loginTitle {
      font-size: 24px;
      font-weight: 600;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .btn {
      width: 100%;
    }
  }
}
.imgse {
  width: 110px;
  height: 40px;
}
</style>
